package com.ohs.ohsoa.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ohs.ohsoa.system.dto.ScheduleDto;
import com.ohs.ohsoa.system.entity.Schedule;
import com.ohs.ohsoa.system.entity.Schedulepersons;
import com.ohs.ohsoa.system.mapper.ScheduleMapper;
import com.ohs.ohsoa.system.vo.ScheduleVo;
import com.ohs.ohsoa.utils.bean.requestBean.PageParam;
import org.springframework.beans.BeanUtils;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {

    @Resource
    private ScheduleMapper scheduleMapper;

    @Resource
    private SchedulepersonsService schedulepersonsService;


    @Resource
    private MessageSource messageSource;

    @Override
    public List<ScheduleVo> scheduleSelectList(PageParam<ScheduleDto> pageParam) {
        // 获取DepartmentDto对象
        ScheduleDto scheduleDto = pageParam.getSearchbean();
        // 分页
        PageHelper.startPage(pageParam.getIndexPage(), pageParam.getPageSize());
        return  scheduleMapper.scheduleSelectList(scheduleDto);
    }

    @Override
    public List<ScheduleVo> scheduleSelectDayInclude(PageParam<ScheduleDto> pageParam) {
        // 获取DepartmentDto对象
        ScheduleDto scheduleDto = pageParam.getSearchbean();
        // 分页
        PageHelper.startPage(pageParam.getIndexPage(), pageParam.getPageSize());
        return  scheduleMapper.scheduleSelectDayInclude(scheduleDto);
    }
    /**
     * 查看详情
     *
     * @param scheduleDto "实例对象"
     * @return 1为成功
     * @author ccj
     * @since  2021年9月24日
     */
    @Override
    public ScheduleVo scheduleSelect(ScheduleDto scheduleDto) {
        return scheduleMapper.scheduleSelect(scheduleDto);
    }

    /**
     * 新增数据
     *
     * @param scheduleDto "实例对象"
     * @return 1为成功
     * @author ccj
     * @since  2021年9月24日
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean scheduleInsert(ScheduleDto scheduleDto) {
        Schedule schedule = new Schedule();
        // 将日期与时间合并
        scheduleDto.setSBtime(scheduleDto.getSBdate() + " " + scheduleDto.getSBtime());
        scheduleDto.setSEtime(scheduleDto.getSEdate() + " " + scheduleDto.getSEtime());
        // 将scheduleDto复制给schedule
        BeanUtils.copyProperties(scheduleDto, schedule);
        // 设置更新时间和插入时间，更新时间为1900-01-01 00:00:00
        schedule.setOsInstime(LocalDateTime.now().toString());
        schedule.setOsUpdtime("1900-01-01 00:00:00");
        schedule.setOsAid(scheduleDto.getUserid());
        this.save(schedule);
        //存储日程发起者
        Schedulepersons schedulepersons = new Schedulepersons();
        schedulepersons.setSId(schedule.getSId());
        schedulepersons.setUserinfoGuid(scheduleDto.getUserid());
        schedulepersons.setSpType("1");
        schedulepersonsService.save(schedulepersons);
        //存储日程抄送者
        if(scheduleDto.getCcuserids() != null && scheduleDto.getCcuserids().size() > 0){
            for (String ccuserid :scheduleDto.getCcuserids()) {
                Schedulepersons ccschedulepersons = new Schedulepersons();
                ccschedulepersons.setSId(schedule.getSId());
                ccschedulepersons.setUserinfoGuid(ccuserid);
                ccschedulepersons.setSpType("2");
                schedulepersonsService.save(ccschedulepersons);
            }
        }
        return true;
    }



    /**
     * 根据日程时间查询所有日程
     *
     * @param  scheduleDto "实例对象"
     * @return 1为成功
     * @author ccj
     * @since  2021年9月24日
     */
    @Override
    public List<ScheduleVo> scheduleSelectAll(ScheduleDto scheduleDto){
        return scheduleMapper.scheduleSelectAll(scheduleDto);
    }

    @Override
    public Boolean scheduleDelete(ScheduleDto scheduleDto) {
        return scheduleMapper.scheduleDelete(scheduleDto.getSIds());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean scheduleUpdate(ScheduleDto scheduleDto) {
        // 将日期与时间合并
        scheduleDto.setSBtime(scheduleDto.getSBdate() + " " + scheduleDto.getSBtime());
        scheduleDto.setSEtime(scheduleDto.getSEdate() + " " + scheduleDto.getSEtime());
        Schedule schedule = this.getById(scheduleDto.getSId());
        QueryWrapper<Schedulepersons> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("s_id",schedule.getSId()).eq("sp_type","2");
        schedulepersonsService.remove(queryWrapper);
        if(scheduleDto.getCcuserids() != null && scheduleDto.getCcuserids().size() > 0){
            for (String ccuserid :scheduleDto.getCcuserids()) {
                Schedulepersons ccschedulepersons = new Schedulepersons();
                ccschedulepersons.setSId(schedule.getSId());
                ccschedulepersons.setUserinfoGuid(ccuserid);
                ccschedulepersons.setSpType("2");
                schedulepersonsService.save(ccschedulepersons);
            }
        }

        BeanUtils.copyProperties(scheduleDto,schedule,"s_remind_minute","os_instime");
        schedule.setOsAid(scheduleDto.getUserid());
        schedule.setOsUpdtime(LocalDateTime.now().toString());
        return this.updateById(schedule);
    }


}
